<?php
namespace app\service;

use app\controller\SaleIncome;
use think\facade\Db;
use app\model\SingleModel;
use app\model\SaleModel;
use app\model\SaleIncomeModel;
use app\model\SaleOutlayModel;
use app\model\ReceiptModel;
use Exception;

class ReceivablesService{  
  

    public function list($params){
        $swhere=[];
        $twhere=[];
        //按出发日期查询
        if(!empty($params['start_date']) && !empty($params['end_date'])){
            if(!empty($params['start_date'])){
                $twhere[] = ['b.start_date','>= time',$params['start_date']];
                $swhere[] = ['a.start_date','>= time',$params['start_date']];  
            }
            if(!empty($params['end_date'])){
                $twhere[] = ['b.start_date','<= time',$params['end_date']];  
                $swhere[] = ['a.start_date','<= time',$params['end_date']];  
            }
        }else{
            return '缺少搜索日期参数';
        }
       
        if(!empty($params['sn'])){
            $swhere[] = ['a.sn','=',$params['sn']];  
            $twhere[] = ['b.sn','=',$params['sn']];  
        }
        
        if(!empty($params['client_id'])){
            $swhere[] = ['a.client_id','=',$params['client_id']];  
            $twhere[] = ['a.client_id','=',$params['client_id']];  
        }        
        if(!empty($params['operator_id'])){
            $twhere[] = ['b.operator_id','=',$params['operator_id']];  
            $swhere[] = ['a.operator_id','=',$params['operator_id']];  
        }
        if(!empty($params['is_review'])){            
            $twhere[] = ['a.is_review','=',$params['is_review']==2?1:0];  
            $swhere[] = ['a.income_review','=',$params['is_review']==2?1:0];  
        }
        $swhere[] = ['a.type','=',7];   
        $single = SingleModel::alias('a')
        ->field('a.id,a.operator_id,a.sell_price,a.num,a.type,a.content as title,a.sn,a.start_date,a.end_date,a.income_review as is_review,a.remark,c.company_name as client,d.cn_name as operator')          
        ->join('client c','a.client_id=c.id','left')
        ->join('user d','a.operator_id=d.id','left')
        ->where($swhere)->buildSql();

        $data = SaleIncomeModel::alias('a')
            ->field('a.id,b.operator_id,a.sell_price,a.num,a.type,b.title,b.sn,b.start_date,b.end_date,a.is_review,a.remark,c.company_name as client,d.cn_name as operator')
            ->join('sale b','a.pid=b.id','left')
            ->join('client c','a.client_id=c.id','left')
            ->join('user d','b.operator_id=d.id','left')
            ->union($single)
            ->where($twhere)->select()->toArray();
        
        
        foreach ($data as $k=>&$v) {  
            $receipts = Db::name('receipt')->where(['type'=>$v['type'],'income_id'=>$v['id']])->sum("money"); 
            $v['total'] = $v['sell_price'] * $v['num'];
            $v['receipt'] = $receipts;
            $v['not_receipt'] = $v['sell_price'] * $v['num'] - $receipts;
            $v['review'] = $v['is_review']?'已审核':'未审核';
            if($params['is_zero']){               
                if($v['sell_price'] * $v['num'] - $receipts == 0){
                    unset($data[$k]);
                    $data = array_values($data);
                }
            }            
        }
        
        return ["list"=>$data,"count"=>count($data)];
    }   

    public function review($params){       
        $count = 0;            
        foreach ($params['list'] as $key => $item) {           
            if(in_array($item['type'], [4,5,6])){
                $saleIncomeModel = new SaleIncomeModel();
                $data=$saleIncomeModel->where('id',$item['id'])->update(['is_review'=>'1']); 
            }else{
                $singleModel = new SingleModel();                
                $data=$singleModel->where('id',$item['id'])->update(['income_review'=>'1']); 
            }
            if($data){
                $count++;
            }            
        }
        return $count;
    }   
    //取消审核
    public function cancel_review($params){    
        $count = 0;
        foreach ($params['list'] as $key => $item) {            
            if(in_array($item['type'], [4,5,6])){
                $saleIncomeModel = new SaleIncomeModel();  
                $data=$saleIncomeModel->where('id',$item['id'])->update(['is_review'=>'0']);                 
            }else{
                $singleModel = new SingleModel();              
                $data=$singleModel->where('id',$item['id'])->update(['income_review'=>'0']);                  
            }
            if($data){
                $count++;
            }            
        }
        return $count;
    }

    public function create_all($params){   
        $ReceiptModel = new ReceiptModel();
        $data = $ReceiptModel->saveAll($params['list']);        
        return $params['list'];
    }

}